package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.paperUtils.ChildRightOrWrongJudgeUtils;
import com.lancoo.answer.util.paperUtils.DecimalFormatUtils;

/**
 * Created by pengmi on 2021/12/14 10:58
 * Description:语境填空题型查看评阅组合view
 */
public class FillContextAnswerResultView extends RelativeLayout {

    private static final String TAG = "AnswerAnalysisView";


    public FillContextAnswerResultView(Context context) {
        super(context);
        init(context);
    }

    public FillContextAnswerResultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FillContextAnswerResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.ev_layout_fill_answer_result, this);
        TextView tv_title = findViewById(R.id.tv_score_des);
        TextView tv_title1 = findViewById(R.id.tv_answer_des);
        TextView tv_title2 = findViewById(R.id.tv_my_score);
        TextView tv_title3 = findViewById(R.id.tv_answer);
        String str = "【";
        float v = tv_title.getPaint().measureText(str);
        tv_title.setTranslationX(-v / 3);
        tv_title1.setTranslationX(-v / 3);
        tv_title2.setTranslationX(-v * 2 / 3);
        tv_title3.setTranslationX(-v * 2 / 3);

    }

    public void setFillResult(Item item) {
        AnswerAnalysisView answerAnalysisView = findViewById(R.id.answer_analysis_view);
        answerAnalysisView.setAnalysis(item.getItemAnalysis());
        answerAnalysisView.adjustFontSize();
        adjustFontSize();
        TextView textView = findViewById(R.id.tv_answer);
        String replaceStr = textView.getResources().getString(R.string.ev_standard_answer_replace);
        String itemAnswer = item.getItemAnswer();
        if (itemAnswer == null) {
            itemAnswer = "";
        }
        textView.setText(itemAnswer.replace("$/", replaceStr).replace("$", ""));
        TextView tv_myScore = findViewById(R.id.tv_my_score);
        TextView tv_scorDes = findViewById(R.id.tv_score_des);
        float evalScore = item.getEvalScore();
        String s = DecimalFormatUtils.RoundToTheNearestDecimalPlace1(evalScore);
        tv_myScore.setText(String.format("%s分", s));
        int state = ChildRightOrWrongJudgeUtils.judateItemAnwerState(item);
        Log.e(TAG, "setFillResult: " + state);
        switch (state) {
            case 0:
                tv_myScore.setTextColor(0xffff4d4d);
                tv_scorDes.setTextColor(0xffff4d4d);
                break;
            case 1:
                tv_myScore.setTextColor(0xff00c063);
                tv_scorDes.setTextColor(0xff00c063);
                break;
            case 2:
                tv_myScore.setText("--");
                tv_myScore.setTextColor(0xff0099ff);
                tv_scorDes.setTextColor(0xff0099ff);
                break;
        }
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if (taskControlBean != null && taskControlBean.getExpired()) {
            tv_myScore.setVisibility(GONE);
            tv_scorDes.setVisibility(GONE);
            TextView tv_title = findViewById(R.id.tv_answer_des);
            LayoutParams layoutParams = (LayoutParams) tv_title.getLayoutParams();
            layoutParams.topMargin = 0;
            tv_title.setLayoutParams(layoutParams);
        }
    }

    public void onlyShowAnalysis() {
        findViewById(R.id.tv_score_des).setVisibility(GONE);
        findViewById(R.id.tv_my_score).setVisibility(GONE);
        findViewById(R.id.tv_answer_des).setVisibility(GONE);
        findViewById(R.id.tv_answer).setVisibility(GONE);
        View view = findViewById(R.id.answer_analysis_view);
        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
        layoutParams.topMargin = 0;
        view.setLayoutParams(layoutParams);
    }

    public void showAnalysisAndAnswer() {
        findViewById(R.id.tv_answer_des).setVisibility(GONE);
        findViewById(R.id.tv_answer).setVisibility(GONE);
    }

    public void adjustFontSize() {
        TextView tvDesScore = findViewById(R.id.tv_score_des);
        TextView tvMyScore = findViewById(R.id.tv_my_score);
        TextView tvAnswerDes = findViewById(R.id.tv_answer_des);
        TextView tvAnswer = findViewById(R.id.tv_answer);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tvDesScore, 14);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tvMyScore, 13);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tvAnswerDes, 14);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tvAnswer, 13);
    }
}
